﻿//------------------------------------------------------------------------------
// <copyright file="LoopbackClient.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <summary>
// A class that contains information about a loopback client.
// </summary>
//------------------------------------------------------------------------------

namespace Microsoft.Internal.GamesTest.Rpc.Tests.RemoteLoopback
{
    // Standard namespaces...
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;

    // User-defined namespaces...
    using Microsoft.Internal.GamesTest.Rpc.Tests.Utilities;

    /// <summary>
    /// A class that contains information about a loopback client.
    /// </summary>
    public class LoopbackClient
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LoopbackClient" /> class.
        /// </summary>
        /// <param name="client">The TCP client of the connection.</param>
        /// <param name="hostName">The name of the host that the connection is with.</param>
        public LoopbackClient(TcpClient client, string hostName = null)
        {
            // Initialize...
            this.Client = client;
            this.IPAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;

            // Attempt to get the host name...
            try
            {
                if (string.IsNullOrEmpty(hostName))
                {
                    this.HostName = Dns.GetHostEntry(this.IPAddress).HostName;
                    int index = this.HostName.IndexOf(".");
                    if (index > 0)
                    {
                        this.HostName = this.HostName.Substring(0, index);
                    }
                }
                else
                {
                    this.HostName = hostName;
                }
            }
            catch
            {
                Console.WriteLine("Failed to resolve the host name for {0}.", this.IPAddress);
                this.HostName = null;
            }
        }

        /// <summary>
        /// Gets the TCP client for the loopback connection.
        /// </summary>
        /// <value>The TCP client for the loopback connection.</value>
        public TcpClient Client { get; private set; }

        /// <summary>
        /// Gets the host name of the loopback client.
        /// </summary>
        /// <value>The host name of the loopback client.</value>
        public string HostName { get; private set; }

        /// <summary>
        /// Gets the IP address of the loopback client.
        /// </summary>
        /// <value>The IP address of the loopback client.</value>
        public IPAddress IPAddress { get; private set; }

        /// <summary>
        /// Requests that the underlying TCP connection be closed.
        /// </summary>
        public void Close()
        {
            if (this.Client.Connected)
            {
                this.Client.Close();
            }
        }

        /// <summary>
        /// Converts the loopback client to a string.
        /// </summary>
        /// <returns>A string that represents the loopback client.</returns>
        public string ToShortString()
        {
            if (string.IsNullOrEmpty(this.HostName))
            {
                return this.IPAddress.ToString();
            }
            else
            {
                return this.HostName;
            }
        }

        /// <summary>
        /// Converts the loopback client to a string.
        /// </summary>
        /// <returns>A string that represents the loopback client.</returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.HostName))
            {
                return string.Format("{0} (Unknown HostName)", this.IPAddress);
            }
            else
            {
                return string.Format("{0} ({1})", this.IPAddress, this.HostName);
            }
        }
    }
}
